package sorting.algorithms;

import java.util.ArrayList;
import java.util.List;

public class HeapSort implements SortingAlgorithm{

	
	public List<Comparable> sortAsc(List<Comparable> comparables) {
		
		List<Comparable> sortedList = new ArrayList<Comparable>();
		Comparable [] compArray = new Comparable[comparables.size()];
		int i = 0;
		
		for(Comparable comparable : comparables) {
			compArray[i] = comparable;
			i++;
		}
		
		BinaryHeap binaryHeap = new BinaryHeap(compArray);
		
		while(!binaryHeap.isEmpty()) {
			sortedList.add(binaryHeap.deleteMin());
		}
		
		List<Comparable> result = new ArrayList<Comparable>();
		for(Comparable comparable : sortedList) {
			result.add(comparable);
		}
		return result;
	}
	
	public void sortDesc(List<Comparable> comparables) {
		
		List<Comparable> sortedList = new ArrayList<Comparable>();
		Comparable [] compArray = new Comparable[comparables.size()];
		int i = 0;
		
		for(Comparable comparable : comparables) {
			compArray[i] = comparable;
			i++;
		}
		
		BinaryHeap binaryHeap = new BinaryHeap(compArray);
		
		while(!binaryHeap.isEmpty()) {
			sortedList.add(binaryHeap.deleteMin());
		}
		
		comparables.clear();
		
		while(!sortedList.isEmpty()) {
			comparables.add(sortedList.remove(sortedList.size()-1));
		}
	}

	@Override
	public List<Comparable> sort(List<Comparable> toSort) {
		return sortAsc(toSort);
	}

	@Override
	public String getName() {
		return "HeapSort";
	}
}
